﻿using Assets.Scripts.Interface;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;

public delegate void FuncListener<T>(T t);//监听委托，一般用于发布碰撞事件供其他对象订阅

/// <summary>
/// 管理游戏进程
/// 存储一些游戏配置属性
/// </summary>
public class GameManager : Singleton<GameManager> {
    #region field
    [Header("World Statu")]
    [Tooltip("所有挂载了jumper控件物体的重力加速度。")]
    [SerializeField]
    public float gravity = 10;
    [Tooltip("地图块的z轴偏移")]
    public float deviation = 1.28f;
    [SerializeField]
    private GameObject sui;//玩家的游戏物体，方便获取
    private Player player;//玩家的脚本
    private MoveControl playerControl;//玩家控制器

    #endregion

    #region 获取UI控件
    [Tooltip("获取过关界面")]
    public GameObject passPanel;
    [SerializeField, Tooltip("加分面板")]
    public Text text;
    [SerializeField, Tooltip("分数")]
    private int score=0;
    #endregion

    #region constField
    //tag标签列表
    public const string ENTITY = "Entity";
    public const string PLAYER = "Player";
    public const string COLLIDEROBJ = "ColliderObj";
    public const string MAP = "Map";
    public const string TRIGGEROBJ = "TriggerObj";
    private Text scoreText;

    #endregion

    #region Properties

    public GameObject Sui//只读
    {
        get
        {
            return sui;
        }
    }
    public Player Player
    {
        get
        {
            return player;
        }
    }

    public MoveControl PlayerControl
    {
        get
        {
            return playerControl;
        }
    }
    #endregion

    public override void FixedAwake()
    {

        //初始化所有字段
        //camare
        GameObject cmGo = GameObject.FindGameObjectWithTag("MainCamera");
        if (cmGo == null) throw new NullReferenceException(name + " Can't find MainCamera by tag");
        //sui
        sui = GameObject.Find("Sui");
        if (sui == null)//判空
        {
            sui = GameObject.FindGameObjectWithTag("Player");
            if (sui == null)
            {
                print("Can't find player");
                throw new System.NullReferenceException("Can't find Player");
            }
        }
        this.ProbeGameObj(ref sui, "Sui", "Player",
            name + " GameManager can't find player sui by name",
            name + " GameManager can't find player sui by Tag");


        //寻找玩家脚本
        player = sui.GetComponent<Player>();
        this.ProbeComponent(ref player, "Player scrip don't find . on" + name);//找不到玩家脚本会抛异常
        //MoveControl
        playerControl = sui.GetComponent<MoveControl>();
        this.ProbeComponent(ref playerControl, "PlayerControl scrip don't find . on" + name);//找不到玩家脚本会抛异常



        //暂停界面和过关界面的初始化
        passPanel = GameObject.Find("Canvas/PassPanel");
        if (GameObject.Find("Canvas/ScoreBg/Text") != null)
            text = GameObject.Find("Canvas/ScoreBg/Text").GetComponent<Text>();
        if (passPanel != null)
        {
            passPanel.SetActive(false);
        }

        Application.targetFrameRate=30;//设置游戏帧率，降低功耗
    }
    public void Stop(MoveControl mControl, bool enableRotate)
    {
        if (mControl != null)
        {
            if (enableRotate)
                mControl.IsChange = !mControl.IsChange;//是否旋转
            #region 控制物体停下来
            if (!mControl.IsStop)
            {
                GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().cullingMask = ~(1 << 8);
                mControl.IsStop = true;
            }
            else
            {
                GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>().cullingMask |= (1 << 8);
                mControl.IsStop = false;
            }
            if (mControl.power == 0)
                mControl.ModifyPower();
            else mControl.power = 0;
            mControl.rig.velocity = new Vector3(0, 0, 0);

            #endregion
            //动画机暂停
            mControl.animator.SetInteger("dir", (int)mControl.dir);//更改动画机的参数
            mControl.animator.SetBool("isIdle", mControl.IsStop);
        }
    }//主角停止事件
    public void Score(int  value)
    {
        score += value;
        text.text = score.ToString();
    }
    public void DeathPanel()
    {
        StartCoroutine(Wait());
        scoreText = GameObject.Find("DeathPanel/Score").GetComponent<Text>();
        scoreText.text = score.ToString();
    }

    private IEnumerator Wait()
    {
        yield return new WaitForSeconds(1);
        UIshower.Instance.StartPlay(Nodeal.DeathPanel);
    }
}
